package app.util.image.action.distort
{
	import app.util.image.action.PartialProcessingAction;
	
	import flash.display.*;
	import flash.filters.*;
	import flash.geom.*;
	
	public class StretchVAction extends PartialProcessingAction
	{	
		public override function getDescription():String
		{
			return "Stretch Vertically";
		}
		
		protected override function doParticalProcess(cur:BitmapData, p:Array, str:Number):BitmapData
		{			
				
			var ret:BitmapData = cur.clone();
			var angle:Number = str / 360;
            var width:int = ret.width;
            var height:int = ret.height;
            var mapData:BitmapData = new BitmapData(width, height, false);
            var scale:Number = 256 / (320 / p[2]);
            
            for(var w:int = 0; w < width; w++){
            	for(var h:int = 0; h < height; h++){
            		mapData.setPixel32(w, h, 0x808080);
            	}
            }
            
            var i:int = -p[2];
            var j:int;
            while (i <= p[2])
            {
                j = -p[2];
                while (j <= p[2])
                {
                    var px:Number = i;
                    var py:Number = j + j * (Math.pow(Math.abs(j) / p[2], angle) - 1) * (1 - Math.abs(i) / p[2]);
                    
                    px = Math.round(128 + 256 * (px - i) / scale);
                    py = Math.round(128 + 256 * (py - j) / scale);
                    
                    mapData.setPixel32(i + p[0], j + p[1], px << 8 | py);
                    j++;
                }
                i++;
            }
            var distortFilter:DisplacementMapFilter = new DisplacementMapFilter(mapData, new Point(0,0),
            							   			 BitmapDataChannel.GREEN, BitmapDataChannel.BLUE, 
            							  			 scale, scale, DisplacementMapFilterMode.IGNORE);
            ret.applyFilter(ret,ret.rect,new Point(0,0), distortFilter);
            return ret;
		}
	}
}